package com.whfc.wxmp.api.fmam.service.impl;

import com.whfc.common.exception.BizException;
import com.whfc.common.file.FileHandler;
import com.whfc.common.file.FilePathConfig;
import com.whfc.common.generator.KeyGeneratorUtil;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.DateUtil;
import com.whfc.common.util.RegexUtil;
import com.whfc.fmam.entity.dto.*;
import com.whfc.fmam.entity.enums.WeighNoteImgType;
import com.whfc.fmam.entity.enums.WeighNoteRecvSendType;
import com.whfc.fmam.entity.enums.WeightNoteSourceType;
import com.whfc.fmam.entity.enums.WeightNoteType;
import com.whfc.fmam.entity.qo.*;
import com.whfc.fmam.service.FmamWeighNoteService;
import com.whfc.fuum.dto.AppProjectDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.entity.WxUser;
import com.whfc.fuum.service.AppProjectService;
import com.whfc.fuum.service.SysUserService;
import com.whfc.wxmp.api.fmam.service.WxFmamWeighNoteService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * @Description:
 * @author: qzexing
 * @version: 1.0
 * @date: 2020/4/8 23:07
 */
@Service
public class WxFmamWeighNoteServiceImpl implements WxFmamWeighNoteService {

    @DubboReference(interfaceClass = FmamWeighNoteService.class, version = "1.0.0")
    private FmamWeighNoteService fmamWeighNoteService;

    @DubboReference(interfaceClass = SysUserService.class, version = "1.0.0")
    private SysUserService sysUserService;

    @DubboReference(interfaceClass = AppProjectService.class, version = "1.0.0")
    private AppProjectService projectService;

    /**
     * 移动收发料图片路径
     */
    private String weighNoteImgOssDir = "weighNote/img";

    @Autowired
    private FileHandler fileHandler;

    @Autowired
    private FilePathConfig filePathConfig;


    @Override
    public PageData<FmamWeighNoteDTO> list(Integer weightNoteType, Integer deptId, Date date, String code,
                                           Integer pageNum, Integer pageSize) {
        WeighNoteListQO qo = new WeighNoteListQO();
        qo.setWeightNoteType(weightNoteType);
        qo.setDeptId(deptId);
        qo.setCode(code);
        qo.setPageNum(pageNum);
        qo.setPageSize(pageSize);
        qo.setStartTime(date);
        qo.setEndTime(date);
        qo.setSourceList(Collections.singletonList(WeightNoteSourceType.CHECK.getValue()));
        return fmamWeighNoteService.list(qo);
    }

    @Override
    public FmamWeighNoteDetailDTO detail(Long id) {
        return fmamWeighNoteService.detail(id);
    }

    @Override
    public FmamWeighNoteTotalDTO getWeighNoteTotal(Integer deptId, Date date, String code) {
        WeighNoteQO weighNoteQO = new WeighNoteQO();
        weighNoteQO.setDeptId(deptId);
        Date startTime = DateUtil.getDateBegin(date);
        Date endTime = DateUtil.getDateEnd(date);
        weighNoteQO.setStartTime(startTime);
        weighNoteQO.setEndTime(endTime);
        weighNoteQO.setCode(code);
        weighNoteQO.setSourceList(Collections.singletonList(WeightNoteSourceType.CHECK.getValue()));
        return fmamWeighNoteService.getWeighNoteTotal(weighNoteQO);
    }

    @Override
    public void add(WxUser wxUser, WeighNoteAddQO weighNoteAddQO) {
        WeightNoteType weightNoteType = WeightNoteType.parseValue(weighNoteAddQO.getWeightNoteType());
        if (weightNoteType == null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "磅单类型错误.");
        }
        if (WeightNoteType.INSTANT.equals(weightNoteType)) {
            if (weighNoteAddQO.getReceiveSupplierId() == null || weighNoteAddQO.getSendSupplierId() == null) {
                throw new BizException(ResultEnum.FAILURE.getCode(), "直入直出类型收料单位与发料单位不能为空.");
            }
        }
        WeighNoteRecvSendType recvSendType = WeighNoteRecvSendType.parseValue(weighNoteAddQO.getRecvSendType());
        if (recvSendType == null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "收发料类型错误.");
        }
        AppProjectDTO project = projectService.getByDeptId(weighNoteAddQO.getDeptId());
        if (project == null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "该项目不存在.");
        }
        if (!RegexUtil.isPlate(weighNoteAddQO.getLicensePlate())) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "该车牌号不存在.");
        }
        //材料判断
        //是否是发料
        boolean isReceived = WeightNoteType.RECEIVED.equals(weightNoteType);
        List<WeighNoteMatAddQO> noteMatList = weighNoteAddQO.getWeighNoteMatList();
        for (WeighNoteMatAddQO weighNoteMatAddQO : noteMatList) {
            Double confirmAmount = weighNoteMatAddQO.getConfirmAmount();
            if (confirmAmount <= 0) {
                throw new BizException(ResultEnum.FAILURE.getCode(), "确认数量必须大于0.");
            }
            Double actualAmount = weighNoteMatAddQO.getActualAmount();
            if (isReceived) {
                if (actualAmount == null || actualAmount <= 0) {
                    throw new BizException(ResultEnum.FAILURE.getCode(), "实际数量必须大于0.");
                }
            }
        }
        weighNoteAddQO.setProjectCode(project.getProjectCode());
        //设置用户
        SysUser user = sysUserService.getUserByPhone(wxUser.getPhone());
        Integer userId;
        String userName;
        if (user != null) {
            userId = user.getId();
            userName = user.getNickname();
        } else {
            userId = wxUser.getId();
            userName = wxUser.getName();
            if (StringUtils.isEmpty(userName)) {
                userName = wxUser.getNickName();
            }
        }
        weighNoteAddQO.setCheckUserId(userId);
        weighNoteAddQO.setCheckUserName(userName);

        fmamWeighNoteService.add(weighNoteAddQO);
    }

    @Override
    public FmamWeighNoteImgDTO uploadImg(Integer imgType, MultipartFile file) {
        WeighNoteImgType noteImgType = WeighNoteImgType.parseValue(imgType);
        if (noteImgType == null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "图片类型错误.");
        }
        String imgUrl = null;
        String ossKey = filePathConfig.getFilePath(weighNoteImgOssDir, KeyGeneratorUtil.genStrId() + ".jpg");
        try {
            imgUrl = fileHandler.upload(ossKey, file.getInputStream());
        } catch (Exception ex) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "上传文件到oss出现异常");
        }
        return fmamWeighNoteService.uploadImg(imgType, imgUrl);
    }


    @Override
    public FmamWeighNoteCountDTO weighNoteCount(Integer deptId) {
        WeighNoteQO weighNoteQO = new WeighNoteQO();
        weighNoteQO.setDeptId(deptId);
        List<Integer> sources = new ArrayList<>();
        sources.add(WeightNoteSourceType.WEIGHING.getValue());
        sources.add(WeightNoteSourceType.MAKE_UP.getValue());
        sources.add(WeightNoteSourceType.CHECK.getValue());
        weighNoteQO.setSourceList(sources);
        return fmamWeighNoteService.weighNoteCount(weighNoteQO);
    }

    @Override
    public FmamWeighNoteTotalDTO recvSendTypeTotalByDay(Integer deptId, Date date) {
        Date startTime = DateUtil.getDateBegin(date);
        Date endTime = DateUtil.getDateEnd(date);
        return recvSendTypeTotal(deptId, startTime, endTime,null);
    }

    @Override
    public FmamWeighNoteTotalDTO recvSendTypeTotalByMonth(Integer deptId, Date month) {
        Date startTime = DateUtil.getMonthBegin(month);
        Date endTime = DateUtil.getMonthEnd(month);
        return recvSendTypeTotal(deptId, startTime, endTime,null);
    }
    @Override
    public FmamWeighNoteTotalDTO recvSendTypeTotalV2(Integer deptId, String date,Integer dateType,Integer source) {
        Date endTime;
        Date startTime;
        Date dateParam;
        switch(dateType){
            case 1:
                //按日
                dateParam= cn.hutool.core.date.DateUtil.parse(date,"yyyy-MM-dd");
                startTime = DateUtil.getDateBegin(dateParam);
                endTime = DateUtil.getDateEnd(dateParam);
                break;
            case 2:
                //按月
                dateParam= cn.hutool.core.date.DateUtil.parse(date,"yyyy-MM");
                startTime = DateUtil.getMonthBegin(dateParam);
                endTime = DateUtil.getMonthEnd(dateParam);
                break;
            default:
                startTime = null;
                endTime = null;
        }
        return recvSendTypeTotal(deptId, startTime, endTime,source);
    }

    @Override
    public FmamWeighNoteTotalDTO recvSendTypeTotal(Integer deptId) {
        return recvSendTypeTotal(deptId,null,null,null);
    }

    @Override
    public PageData<FmamWeighNoteDTO> listByRecvSendTypeAndDay(Integer deptId, Date date, Integer weighNoteType, Integer recvSendType, Integer pageSize, Integer pageNum,Integer source) {
        WeighNoteListQO weighNoteListQO = new WeighNoteListQO();
        weighNoteListQO.setDeptId(deptId);
        weighNoteListQO.setWeightNoteType(weighNoteType);
        Date startTime = DateUtil.getDateBegin(date);
        Date endTime = DateUtil.getDateEnd(date);
        weighNoteListQO.setStartTime(startTime);
        weighNoteListQO.setEndTime(endTime);
        if (recvSendType != null) {
            weighNoteListQO.setRecvSendTypeList(Collections.singletonList(recvSendType));
        }
        //磅单来源
        List<Integer> sourceList = new ArrayList<>();
        if(null == source){
            sourceList.add(WeightNoteSourceType.WEIGHING.getValue());
            sourceList.add(WeightNoteSourceType.MAKE_UP.getValue());
            sourceList.add(WeightNoteSourceType.CHECK.getValue());
        }else{
            sourceList.add(source);
        }
        weighNoteListQO.setSourceList(sourceList);
        weighNoteListQO.setPageNum(pageNum);
        weighNoteListQO.setPageSize(pageSize);
        return fmamWeighNoteService.listByRecvSendTypeAndDay(weighNoteListQO);
    }

    @Override
    public PageData<FmamWeighNoteDTO> listByRecvSendTypeAndMonth(Integer deptId, Date month, Integer weighNoteType, Integer recvSendType, Integer pageSize, Integer pageNum,Integer source) {
        WeighNoteListQO weighNoteListQO = new WeighNoteListQO();
        weighNoteListQO.setDeptId(deptId);
        weighNoteListQO.setWeightNoteType(weighNoteType);
        Date startTime = DateUtil.getMonthBegin(month);
        Date endTime = DateUtil.getMonthEnd(month);
        weighNoteListQO.setStartTime(startTime);
        weighNoteListQO.setEndTime(endTime);
        if (recvSendType != null) {
            weighNoteListQO.setRecvSendTypeList(Collections.singletonList(recvSendType));
        }
        //磅单来源
        List<Integer> sourceList = new ArrayList<>();
        if(null == source){
            sourceList.add(WeightNoteSourceType.WEIGHING.getValue());
            sourceList.add(WeightNoteSourceType.MAKE_UP.getValue());
            sourceList.add(WeightNoteSourceType.CHECK.getValue());
        }else{
            sourceList.add(source);
        }
        weighNoteListQO.setSourceList(sourceList);
        weighNoteListQO.setPageNum(pageNum);
        weighNoteListQO.setPageSize(pageSize);
        return fmamWeighNoteService.listByRecvSendTypeAndMonth(weighNoteListQO);
    }

    @Override
    public PageData<FmamWeighNoteDTO> listByRecvSendType(Integer deptId, Integer weighNoteType, Integer recvSendType, Integer pageSize, Integer pageNum,Integer source) {
        WeighNoteListQO weighNoteListQO = new WeighNoteListQO();
        weighNoteListQO.setDeptId(deptId);
        weighNoteListQO.setWeightNoteType(weighNoteType);
        if (recvSendType != null) {
            weighNoteListQO.setRecvSendTypeList(Collections.singletonList(recvSendType));
        }
        //磅单来源
        List<Integer> sourceList = new ArrayList<>();
        if(null == source){
            sourceList.add(WeightNoteSourceType.WEIGHING.getValue());
            sourceList.add(WeightNoteSourceType.MAKE_UP.getValue());
            sourceList.add(WeightNoteSourceType.CHECK.getValue());
        }else{
            sourceList.add(source);
        }
        weighNoteListQO.setSourceList(sourceList);
        weighNoteListQO.setPageNum(pageNum);
        weighNoteListQO.setPageSize(pageSize);
        return fmamWeighNoteService.listByRecvSendType(weighNoteListQO);
    }

    @Override
    public ListData<FmamWeighNoteAnalysisDTO> analysisByDate(Integer deptId, Date date, Integer weighNoteType, Integer source, Integer recvSendType, Long matTypeId) {
        WeighNoteQO weighNoteQO = new WeighNoteQO();
        Date startTime = DateUtil.getDateBegin(date);
        Date endTime = DateUtil.getDateEnd(date);
        weighNoteQO.setDeptId(deptId);
        weighNoteQO.setStartTime(startTime);
        weighNoteQO.setEndTime(endTime);
        weighNoteQO.setWeightNoteType(weighNoteType);
        List<Integer> sources = new ArrayList<>();
        if (source != null) {
            sources.add(source);
        } else {
            sources.add(WeightNoteSourceType.MAKE_UP.getValue());
            sources.add(WeightNoteSourceType.WEIGHING.getValue());
            sources.add(WeightNoteSourceType.CHECK.getValue());
        }
        weighNoteQO.setSourceList(sources);
        if (recvSendType != null) {
            weighNoteQO.setRecvSendTypeList(Collections.singletonList(recvSendType));
        }
        weighNoteQO.setMatTypeId(matTypeId);
        return fmamWeighNoteService.analysis(weighNoteQO);
    }

    @Override
    public ListData<FmamWeighNoteAnalysisDTO> analysisByMonth(Integer deptId, Date month, Integer weighNoteType, Integer source, Integer recvSendType, Long matTypeId) {
        WeighNoteQO weighNoteQO = new WeighNoteQO();
        Date startTime = DateUtil.getMonthBegin(month);
        Date endTime = DateUtil.getMonthEnd(month);
        weighNoteQO.setDeptId(deptId);
        weighNoteQO.setStartTime(startTime);
        weighNoteQO.setEndTime(endTime);
        weighNoteQO.setWeightNoteType(weighNoteType);
        List<Integer> sources = new ArrayList<>();
        if (source != null) {
            sources.add(source);
        } else {
            sources.add(WeightNoteSourceType.MAKE_UP.getValue());
            sources.add(WeightNoteSourceType.WEIGHING.getValue());
            sources.add(WeightNoteSourceType.CHECK.getValue());
        }
        weighNoteQO.setSourceList(sources);
        if (recvSendType != null) {
            weighNoteQO.setRecvSendTypeList(Collections.singletonList(recvSendType));
        }
        weighNoteQO.setMatTypeId(matTypeId);
        return fmamWeighNoteService.analysis(weighNoteQO);
    }

    @Override
    public ListData<FmamWeighNoteAnalysisDTO> analysis(Integer deptId, Integer weighNoteType, Integer source, Integer recvSendType, Long matTypeId) {
        WeighNoteQO weighNoteQO = new WeighNoteQO();
        weighNoteQO.setDeptId(deptId);
        weighNoteQO.setWeightNoteType(weighNoteType);
        List<Integer> sources = new ArrayList<>();
        if (source != null) {
            sources.add(source);
        } else {
            sources.add(WeightNoteSourceType.MAKE_UP.getValue());
            sources.add(WeightNoteSourceType.WEIGHING.getValue());
            sources.add(WeightNoteSourceType.CHECK.getValue());
        }
        weighNoteQO.setSourceList(sources);
        if (recvSendType != null) {
            weighNoteQO.setRecvSendTypeList(Collections.singletonList(recvSendType));
        }
        weighNoteQO.setMatTypeId(matTypeId);
        return fmamWeighNoteService.analysis(weighNoteQO);
    }

    @Override
    public FmamRecvSendAnaDTO analysisV2(Integer deptId, Integer weighNoteType, Integer source, Integer recvSendType, Long matTypeId,String date,Integer dateType) {
//        WeighNoteQO weighNoteQO = new WeighNoteQO();
//        weighNoteQO.setDeptId(deptId);
//        weighNoteQO.setWeightNoteType(weighNoteType);
        List<Integer> sources = new ArrayList<>();
        if (source != null) {
            sources.add(source);
        } else {
            sources.add(WeightNoteSourceType.MAKE_UP.getValue());
            sources.add(WeightNoteSourceType.WEIGHING.getValue());
            sources.add(WeightNoteSourceType.CHECK.getValue());
        }
        Date endTime;
        Date startTime;
        Date dateParam;
        switch(dateType){
            case 1:
                //按日
                dateParam= cn.hutool.core.date.DateUtil.parse(date,"yyyy-MM-dd");
                startTime = DateUtil.getDateBegin(dateParam);
                endTime = DateUtil.getDateEnd(dateParam);
                break;
            case 2:
                //按月
                dateParam= cn.hutool.core.date.DateUtil.parse(date,"yyyy-MM");
                startTime = DateUtil.getMonthBegin(dateParam);
                endTime = DateUtil.getMonthEnd(dateParam);
                break;
            default:
                startTime = null;
                endTime = null;
        }
//        weighNoteQO.setSourceList(sources);
//        if (recvSendType != null) {
//            weighNoteQO.setRecvSendTypeList(Collections.singletonList(recvSendType));
//        }
//        weighNoteQO.setMatTypeId(matTypeId);
        RecvSendAnalysisQO recvSendAnalysisQO = new RecvSendAnalysisQO();
        recvSendAnalysisQO.setDeptId(deptId);
        recvSendAnalysisQO.setSourceList(sources);
        if (recvSendType != null) {
            recvSendAnalysisQO.setRecvSendTypeList(Collections.singletonList(recvSendType));
        }
        recvSendAnalysisQO.setType(weighNoteType);
        recvSendAnalysisQO.setMatTypeId(matTypeId);
        recvSendAnalysisQO.setStartTime(startTime);
        recvSendAnalysisQO.setEndTime(endTime);
        FmamRecvSendAnaDTO fmamRecvSendAnaDTO = fmamWeighNoteService.analysisByMat(recvSendAnalysisQO);
        return fmamRecvSendAnaDTO;
    }

    @Override
    public PageData<FmamWeighNoteDTO> analysisListByDate(Integer deptId, Long matId, Date date, Integer weighNoteType, Integer recvSendType, String code, Integer source, Integer pageSize, Integer pageNum) {
        WeighNoteListQO weighNoteListQO = new WeighNoteListQO();
        weighNoteListQO.setDeptId(deptId);
        weighNoteListQO.setMatId(matId);
        Date startTime = DateUtil.getDateBegin(date);
        Date endTime = DateUtil.getDateEnd(date);
        weighNoteListQO.setStartTime(startTime);
        weighNoteListQO.setEndTime(endTime);
        weighNoteListQO.setWeightNoteType(weighNoteType);
        if (recvSendType != null) {
            weighNoteListQO.setRecvSendTypeList(Collections.singletonList(recvSendType));
        }
        weighNoteListQO.setCode(code);
        List<Integer> sources = new ArrayList<>();
        if (source != null) {
            sources.add(source);
        } else {
            sources.add(WeightNoteSourceType.MAKE_UP.getValue());
            sources.add(WeightNoteSourceType.WEIGHING.getValue());
            sources.add(WeightNoteSourceType.CHECK.getValue());
        }
        weighNoteListQO.setSourceList(sources);
        weighNoteListQO.setPageNum(pageNum);
        weighNoteListQO.setPageSize(pageSize);
        return fmamWeighNoteService.listByMatId(weighNoteListQO);
    }

    @Override
    public PageData<FmamWeighNoteDTO> analysisListByMonth(Integer deptId, Long matId, Date month, Integer weighNoteType, Integer recvSendType, String code, Integer source, Integer pageSize, Integer pageNum) {
        WeighNoteListQO weighNoteListQO = new WeighNoteListQO();
        weighNoteListQO.setDeptId(deptId);
        weighNoteListQO.setMatId(matId);
        Date startTime = DateUtil.getMonthBegin(month);
        Date endTime = DateUtil.getMonthEnd(month);
        weighNoteListQO.setStartTime(startTime);
        weighNoteListQO.setEndTime(endTime);
        weighNoteListQO.setWeightNoteType(weighNoteType);
        if (recvSendType != null) {
            weighNoteListQO.setRecvSendTypeList(Collections.singletonList(recvSendType));
        }
        weighNoteListQO.setCode(code);
        List<Integer> sources = new ArrayList<>();
        if (source != null) {
            sources.add(source);
        } else {
            sources.add(WeightNoteSourceType.MAKE_UP.getValue());
            sources.add(WeightNoteSourceType.WEIGHING.getValue());
            sources.add(WeightNoteSourceType.CHECK.getValue());
        }
        weighNoteListQO.setSourceList(sources);
        weighNoteListQO.setPageNum(pageNum);
        weighNoteListQO.setPageSize(pageSize);
        return fmamWeighNoteService.listByMatId(weighNoteListQO);
    }

    @Override
    public PageData<FmamWeighNoteDTO> analysisList(Integer deptId, Long matId, Integer weighNoteType, Integer recvSendType, String code, Integer source, Integer pageSize, Integer pageNum) {
        WeighNoteListQO weighNoteListQO = new WeighNoteListQO();
        weighNoteListQO.setDeptId(deptId);
        weighNoteListQO.setMatId(matId);
        weighNoteListQO.setWeightNoteType(weighNoteType);
        if (recvSendType != null) {
            weighNoteListQO.setRecvSendTypeList(Collections.singletonList(recvSendType));
        }
        weighNoteListQO.setCode(code);
        List<Integer> sources = new ArrayList<>();
        if (source != null) {
            sources.add(source);
        } else {
            sources.add(WeightNoteSourceType.MAKE_UP.getValue());
            sources.add(WeightNoteSourceType.WEIGHING.getValue());
            sources.add(WeightNoteSourceType.CHECK.getValue());
        }
        weighNoteListQO.setSourceList(sources);
        weighNoteListQO.setPageNum(pageNum);
        weighNoteListQO.setPageSize(pageSize);
        return fmamWeighNoteService.listByMatId(weighNoteListQO);
    }

    @Override
    public PageData<FmamWeighNoteDTO> analysisListV2(Integer deptId, Long matTypeId, Integer weighNoteType, Integer recvSendType, String code, Integer source, Integer pageSize, Integer pageNum,Integer dateType,String date) {
        WeighNoteListQO weighNoteListQO = new WeighNoteListQO();
        weighNoteListQO.setDeptId(deptId);
        weighNoteListQO.setMatTypeId(matTypeId);
        Date endTime;
        Date startTime;
        Date dateParam;
        if(null != dateType){
            switch(dateType){
                case 1:
                    //按日
                    dateParam= cn.hutool.core.date.DateUtil.parse(date,"yyyy-MM-dd");
                    startTime = DateUtil.getDateBegin(dateParam);
                    endTime = DateUtil.getDateEnd(dateParam);
                    break;
                case 2:
                    //按月
                    dateParam= cn.hutool.core.date.DateUtil.parse(date,"yyyy-MM");
                    startTime = DateUtil.getMonthBegin(dateParam);
                    endTime = DateUtil.getMonthEnd(dateParam);
                    break;
                default:
                    startTime = null;
                    endTime = null;
            }
        }else{
            startTime = null;
            endTime = null;
        }
        weighNoteListQO.setStartTime(startTime);
        weighNoteListQO.setEndTime(endTime);
        weighNoteListQO.setWeightNoteType(weighNoteType);
        if (recvSendType != null) {
            weighNoteListQO.setRecvSendTypeList(Collections.singletonList(recvSendType));
        }
        weighNoteListQO.setCode(code);
        List<Integer> sources = new ArrayList<>();
        if (source != null) {
            sources.add(source);
        } else {
            sources.add(WeightNoteSourceType.MAKE_UP.getValue());
            sources.add(WeightNoteSourceType.WEIGHING.getValue());
            sources.add(WeightNoteSourceType.CHECK.getValue());
        }
        weighNoteListQO.setSourceList(sources);
        weighNoteListQO.setPageNum(pageNum);
        weighNoteListQO.setPageSize(pageSize);
        return fmamWeighNoteService.listByMatId(weighNoteListQO);
    }

    @Override
    public PageData<FmamWeighNoteDailyDTO> daily(Integer deptId, Date time, Integer weighNoteType, Integer pageSize, Integer pageNum) {
        Date startTime = null;
        Date endTime = null;
        if (time != null) {
            startTime = DateUtil.getDateBegin(time);
            endTime = DateUtil.getDateEnd(time);
        }
        return fmamWeighNoteService.daily(deptId, weighNoteType, startTime, endTime, pageNum, pageSize);
    }

    /**
     * 统计收发料数量
     *
     * @param deptId    deptId
     * @param startTime startTime
     * @param endTime   endTime
     * @return FmamWeighNoteTotalDTO
     */
    private FmamWeighNoteTotalDTO recvSendTypeTotal(Integer deptId, Date startTime, Date endTime,Integer source) {
        WeighNoteQO weighNoteQO = new WeighNoteQO();
        weighNoteQO.setDeptId(deptId);
        weighNoteQO.setStartTime(startTime);
        weighNoteQO.setEndTime(endTime);
        //磅单来源
        List<Integer> sources = new ArrayList<>();
        if(null == source){
            sources.add(WeightNoteSourceType.WEIGHING.getValue());
            sources.add(WeightNoteSourceType.MAKE_UP.getValue());
            sources.add(WeightNoteSourceType.CHECK.getValue());
        }else{
            sources.add(source);
        }
        weighNoteQO.setSourceList(sources);
        return fmamWeighNoteService.recvSendTypeTotalByDay(weighNoteQO);
    }


    @Override
    public PageData<FmamWeighNoteDTO> listWarnByRecvSendType(Integer deptId,  Integer weighNoteType,  Integer pageSize, Integer pageNum) {
        WeighNoteListQO weighNoteListQO = new WeighNoteListQO();
        weighNoteListQO.setDeptId(deptId);
        weighNoteListQO.setWeightNoteType(weighNoteType);
        if(null == pageSize)pageSize=1;
        if(null == pageNum)pageNum=1;



        List<Integer> sourceList = new ArrayList<>();
        sourceList.add(WeightNoteSourceType.WEIGHING.getValue());
        sourceList.add(WeightNoteSourceType.MAKE_UP.getValue());
        sourceList.add(WeightNoteSourceType.CHECK.getValue());
        weighNoteListQO.setSourceList(sourceList);
        weighNoteListQO.setPageNum(pageNum);
        weighNoteListQO.setPageSize(pageSize);
        weighNoteListQO.setDiffResultList(Arrays.asList(1,-1));
        return fmamWeighNoteService.listByRecvSendTypeAndDay(weighNoteListQO);
    }


}
